package com.park.web.controller.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.park.client.model.pub.SensitiveWordModel;
import com.park.client.model.question.QuestionModel;
import com.park.client.model.question.QuestionAnswerModel;
import com.park.client.model.question.QuestionTypeModel;
import com.park.client.model.user.UserModel;
import com.park.client.service.pub.SensitiveWordService;
import com.park.client.service.question.QuestionAnswerService;
import com.park.client.service.question.QuestionService;
import com.park.client.service.question.QuestionTypeService;
import com.park.common.util.Page;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.util.SessionUtil;

@Controller
@RequestMapping("/user/question")
public class UserQuestionController extends BaseController{
	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionAnswerService questionAnswerService;
	@Autowired
	private QuestionTypeService questionTypeService;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	
	@RequestMapping("questionManage")
	public String questionManage(Integer pageNow,HttpServletRequest request,HttpSession session){
			List<QuestionModel> questions =	null;
			QuestionModel question = new QuestionModel();
			question.setCreater(SessionUtil.getUserID(session));
			Integer total = questionService.selectTotal(question);
			Page page = new Page(pageNow,total,10);
			question.setStartPos(page.getStartPos());
			questions = questionService.selectPagination(question);
			request.setAttribute("total", total);
			request.setAttribute("page", page);
			request.setAttribute("questions", questions);
			List<QuestionTypeModel> questionTypes =  questionTypeService.selectAll();
			request.setAttribute("questionTypes", questionTypes);
			return Global.USER_PATH+"question/questionManage";
	}
	
	
	/**
	 * 删除
	 */
	@RequestMapping("deleteQuestion")
	public String deleteQuestion(Long questionId){
		questionService.deleteByPrimaryKey(questionId);
		return "redirect:questionManage";
	}
	
	/**
	 * 添加或修改保存
	 */
	@RequestMapping("saveQuestion")
	public String saveQuestion(QuestionModel question,HttpSession session){
		UserModel user = (UserModel) session.getAttribute("user");
		if(question.getId() != null){
			questionService.updateByPrimaryKeySelective(question);
		}else{
			//过滤敏感词
			String questionDetail = question.getQuestionDetail();
			List<SensitiveWordModel> words = sensitiveWordService.selectAll();
			for (SensitiveWordModel word : words) {
				questionDetail = questionDetail.replaceAll(word.getWord(), "***");
			}
			question.setQuestionDetail(questionDetail);
			question.setCreater(user.getId());
			question.setCreateDate(new Date());
			questionService.insertSelective(question);
		}
		return "redirect:questionManage";
	}
	

	/**
	 *修改页面
	 */
	@RequestMapping("toEditQuestion")
	public String toEditQuestion(Long questionId,HttpServletRequest request){
		QuestionModel question = 	questionService.selectByPrimaryKey(questionId);	
		request.setAttribute("question", question);
		List<QuestionTypeModel> questionTypes =  questionTypeService.selectAll();
		request.setAttribute("questionTypes", questionTypes);
		return Global.USER_PATH+"question/editQuestion";
	}
	
	
	/**
	 * 关闭问题
	 */
	@RequestMapping("closeQuestion")
	public String closeQuestion(Long questionId){
		QuestionModel question = questionService.selectByPrimaryKey(questionId);
		question.setIsFin(1);
		questionService.updateByPrimaryKeySelective(question);
		return "redirect:questionManage";
	}
	
	/**
	 * 回答列表
	 */
	@RequestMapping("showAnswers")
	public String showAnswers(Integer pageNow,Long questionId,HttpServletRequest request){
		QuestionAnswerModel questionAnswer = new QuestionAnswerModel();
		questionAnswer.setQuestionId(questionId);
		Integer total = questionAnswerService.selectTotal(questionAnswer);
		Page page = new Page(pageNow,total,10);
		questionAnswer.setStartPos(page.getStartPos());
		List<QuestionAnswerModel> questionAnswers =  questionAnswerService.selectPagination(questionAnswer);
		request.setAttribute("total", total);
		request.setAttribute("page", page);
		request.setAttribute("questionAnswers", questionAnswers);
		return Global.USER_PATH+"question/questionAnswers";
		}
	
	/**
	 * 删除问题对应的回答
	 */
	@RequestMapping("deleteQuestionAnswer")
	public String deleteQuestionAnswer(Long questionAnswerId,Integer questionId){
		questionAnswerService.deleteByPrimaryKey(questionAnswerId);
		return "redirect:showAnswers?questionId="+questionId;
	}

}
